﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bus.console.service.bus.vo;
using System.Web.Script.Serialization;
using NHibernate.Criterion;
using com.console.generic.util;
using web.ui.Service_Ad;

public partial class Service_Ad : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            doAction();
        }
    }

    private void doAction()
    {
        String ad_id = Request.QueryString["ad_id"];
        String obj_id = Request.QueryString["obj_id"];

        if (String.IsNullOrEmpty(ad_id) && String.IsNullOrEmpty(obj_id) )
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AdvertisementVO>();
            dCriteria.Add(Expression.Eq("PopStatus", 1));
            dCriteria.Add(Expression.Le("StartDate",ConvertUtil.ToDateTimeMax(DateTime.Now)));
            dCriteria.Add(Expression.Ge("EndDate", ConvertUtil.ToDateTimeMin(DateTime.Now)));
            dCriteria.Add(Expression.IsNull("DelDT"));

            IList<AdvertisementVO> adList = busService.myService.ExecutableDetachedCriteria<AdvertisementVO>(dCriteria);
    
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            ResultShowVO vo = new ResultShowVO();
            vo.ad = toShowAd(adList);         
            String json = serializer.Serialize(vo);
            Response.Write(json);
            Response.End();
            return;
        }
         if (!String.IsNullOrEmpty(ad_id)) 
        {


            DetachedCriteria dCriteria = DetachedCriteria.For<AdvertisementVO>();
            dCriteria.Add(Expression.Eq("ID", ad_id));         
            dCriteria.Add(Expression.IsNull("DelDT"));
            IList<AdvertisementVO> adList = busService.myService.ExecutableDetachedCriteria<AdvertisementVO>(dCriteria);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ResultShowBVO vo = new ResultShowBVO();
            vo.ad = toShowAdB(adList);
            String json = serializer.Serialize(vo);
            Response.Write(json);
            Response.End();
            return;
        }
         if (!String.IsNullOrEmpty(obj_id))
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<AdvertisementVO>();
            dCriteria.Add(Expression.Eq("PopStatus", 1));
            dCriteria.Add(Expression.Le("StartDate", ConvertUtil.ToDateTimeMax(DateTime.Now)));
            dCriteria.Add(Expression.Ge("EndDate", ConvertUtil.ToDateTimeMin(DateTime.Now)));
            dCriteria.Add(Expression.Eq("SourceID", obj_id));
            dCriteria.Add(Expression.IsNull("DelDT"));

            IList<AdvertisementVO> adList = busService.myService.ExecutableDetachedCriteria<AdvertisementVO>(dCriteria);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            ResultShowVO vo = new ResultShowVO();
            vo.ad = toShowAd(adList);
            String json = serializer.Serialize(vo);
            Response.Write(json);
            Response.End();
            return;
        }
      
    }

    private IList<AdvertisementShowBVO> toShowAdB(IList<AdvertisementVO> adList)
    {
        IList<AdvertisementShowBVO> list = new List<AdvertisementShowBVO>();


        if (adList != null && adList.Count > 0)
        {
            foreach (AdvertisementVO vo in adList)
            {
                AdvertisementShowBVO s = new AdvertisementShowBVO();
                s.id = vo.ID;
                s.obj_id = vo.SourceID;
                s.pop_status = vo.PopStatus.ToString();
                s.title = vo.Title;
                s.content = vo.Content;
                s.img = vo.ImgPath;               
                list.Add(s);
            }
        }
        return list;
    }

    private IList<AdvertisementShowVO> toShowAd(IList<AdvertisementVO> adList)
    {
        IList<AdvertisementShowVO> list = new List<AdvertisementShowVO>();

        if (adList != null && adList.Count > 0) 
        {
            foreach (AdvertisementVO vo in adList) 
            {
                AdvertisementShowVO s = new AdvertisementShowVO();
                s.id = vo.ID;
                s.obj_id = vo.SourceID;
                s.pop_status = vo.PopStatus.ToString();
                s.title = vo.Title;
                list.Add(s);
            }
        }
        return list;
    }
}
namespace web.ui.Service_Ad 
{
    class ResultShowVO 
    {
        public IList<AdvertisementShowVO> ad { get; set; }   
    }

    class ResultShowBVO
    {
        public IList<AdvertisementShowBVO> ad { get; set; }
    }

    class AdvertisementShowVO 
    {
        public String id  {get;set;}

      
        public string title { get; set; }

        public  string pop_status { get; set; }

        public string obj_id { get; set; }
    }


    class AdvertisementShowBVO
    {
        public String id { get; set; }


        public string title { get; set; }

        public string content { get; set; }

        public string img { get; set; }

        public string pop_status { get; set; }

        public string obj_id { get; set; }

    }
  
}